<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字：" prop="vin">
        <el-input v-model="queryParams.vin" placeholder="请输入名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
         >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">

      <el-table-column label="车牌号" align="center" prop="numberPlate" />
      <el-table-column label="日期" align="center" prop="purchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="车主姓名" align="center" prop="ownerName" />
      <el-table-column label="车主电话" align="center" prop="ownerPhone" />

      <el-table-column label="检测站" align="center" prop="detectionstationName" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-if="scope.row.userId == scope.row.assignee && scope.row.actStatus != 0">审批</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAct(scope.row)"
            v-if="scope.row.createrid == scope.row.userId && scope.row.actStatus == 0">提交</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleFlowRecord(scope.row)"
             v-if="scope.row.actStatus != 0">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
           >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <to-do :taskId="taskId" :actId="actId" :businessId="businessId" :backUrl="backUrl" :key="componentKey"></to-do>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/info/info";
import { definitionStart, flowXmlAndNode } from "@/api/flowable/definition";
import ToDo from "../flowable/task/todo/detail/index.vue";
export default {
  name: "Info",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { ToDo },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      taskId: null,
      componentKey: 0,
      actId: '1',
      businessId: null,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      backUrl: "/infos",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        vin: null,
        purchaseDate: null,
        productionMonth: null,
        vehicleType: null,
        detectionDate: null,
        expandId: null,
        ownerId: null,
        ownerName: null,
        ownerPhone: null,
        detectionstationId: null,
        detectionstationName: null,
        dimensionOne: null,
        dimensionTwo: null,
        dimensionThree: null,
        dimensionFour: null,
        dimensionFive: null,
        source: null,
        createrid: null,
        opid: null,
        tokerDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleAct(row) {
      // 流程id 业务id act_data_echo表id
      definitionStart('flow_8htxei0m:1:85004', row.vehicleId, '1').then(res => {
        this.$modal.msgSuccess(res.msg);
        this.getList()
      })
    },
    /** 流程流转记录 */
    handleFlowRecord(row) {
      this.$router.push({
        path: '/flowable/task/finished/detail/index',

        query: {
          procInsId: row.procDefId,
          deployId: row.deployId,
          taskId: row.taskId,
          businessId: row.vehicleId
        }
      })
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleId: null,
        numberPlate: null,
        vin: null,
        purchaseDate: null,
        productionMonth: null,
        vehicleType: null,
        detectionDate: null,
        delFlag: null,
        expandId: null,
        ownerId: null,
        ownerName: null,
        ownerPhone: null,
        detectionstationId: null,
        detectionstationName: null,
        dimensionOne: null,
        dimensionTwo: null,
        dimensionThree: null,
        dimensionFour: null,
        dimensionFive: null,
        source: null,
        createrid: null,
        opid: null,
        tokerDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.componentKey += 1
      const vehicleId = row.vehicleId || this.ids;
      // this.businessId = row.vehicleId
      // this.taskId = row.taskId
      // this.open = true;
      // this.title = "修改【请填写功能名称】";
      this.$router.push({
        path: '/flowable/task/todo/detail/index',
        query: {
          businessId: row.vehicleId,
          taskId: row.taskId,
          actId: this.actId
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + vehicleIds + '"的数据项？').then(function () {
        return delInfo(vehicleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
